home *** CD-ROM | disk | FTP | other *** search
- Path: aloha.com!jching
- From: jching@aloha.com (Jimen Ching)
- Newsgroups: comp.lang.c
- Subject: Re: COLLEGE PROFESSORS! #
- Date: 21 Apr 1996 08:48:47 GMT
- Organization: Coconut Wireless
- Distribution: world
- Message-ID: <4lcspf$cq7@news.aloha.com>
- References: <Pine.A32.3.91.960417134130.26474G-100000@red.weeg.uiowa.edu <8BEE4D1.02C700325C.uuout@sourcebbs.com>
- NNTP-Posting-Host: mango.aloha.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- DAVID MOHORN (david.mohorn@sourcebbs.com) wrote:
- >I agree. There are times when writing such code that readability is
- >more important than efficiency, but the logic of this code was
- >illogical, verified data that was already identified as being invalid,
- >so why continue scanning the data... I think a construct such as:
-
- If it means anything, I agree with you. I think the example your
- professor provided was not the best to use. Though I too consider
- readability to be very important. I also believe that you can have
- your cake and each it too. I.e. your code could be both readable and
- efficient.
-
- [ code example deleted ]
-
- My first rule of programming is "Never say never." Such rules as:
- Never use goto's
- Never have multiple exit points
- Never nest if statements
-
- I simply don't understand why people have such rules. The rule should
- be "Know when to use..., and when not to..."
-
- You've already showed the 'multiple exit points'. If you don't like
- that, here are a few alternatives, using the other *nevers*.
-
- Status = "GOOD"
- if (A) goto ErrorExitPoint
- else process_data
-
- if (B) goto ErrorExitPoint
- else process_data
-
- if (C) goto ErrorExitPoint
- else process_data
- goto ExitPoint
-
- ErrorExitPoint:
- Status = "BAD"
-
- ExitPoint:
- return Status
-
- If you don't like goto's (use nested if's)...
-
- Status = "GOOD"
- if (not A) then
- process_data
- if (not B) then
- process_data
- if (not C) then
- process_data
- else
- Status = "BAD"
- endif
- else
- Status = "BAD"
- endif
- else
- Status = "BAD"
- endif
- return Status
-
- And if you don't like nested if's...
-
- Status="GOOD"
- if (not A) then
- Status = process_data_and_test_B_and_C (B, C)
- else
- Status = "BAD"
- return Status
-
- process_data_and_test_B_and_C (B, C)
- Status = "GOOD"
- process_data
- if (not B) then
- Status = process_data_and_test_C (C)
- else
- Status = "BAD"
- endif
- return Status
-
- process_data_and_test_C (C)
- Status = "GOOD"
- process_data
- if (not C)
- process_data
- else
- Status = "BAD"
- endif
- return Status
-
- This implementation does not use goto's, does not have multiple exit
- points, and does not use nested if statements. It is also very easy to
- read. Not only that, it is also modular.
-
- > I retorted his remark and said it would on a multitasking system where
- > every cycle counts! If I were validating 10,000 data items, example 1
- > would take a considerable amount of time longer than example 2!
-
- First of all. This little example is not a good candidate for measuring
- performance. I.e. example 2 is only faster if the first condition is
- true for a large amount of iterations. If all test conditions were false
- (i.e. process_data is called every time), there would be no difference.
- Even if you used some random distribution for the test conditions,
- the performance might not matter much if process_data is taking most
- of the time. To be a good software engineer (I'm assuming you're not
- a simple programmer), you must consider the situation very carefully
- and select your optimization points that give you the most benefit.
- But, if this is the most inner loop of your program. Then saving two
- extra comparisons could mean a significant performance boost. Especially
- in RISC systems that do not have good branch prediction hardware. I
- don't have numbers to back me up, but I have tested one program where
- a simple loop killed the entire program (made it slow as hell).
-
- >RN> In this day and age of Alphas, Pentiums, PowerPCs, MIPSs, and home
- > >computers with 24MB of interleaved DIMM RAM, which multitasking systems are
- > >those? Do you honestly believe that Windows code squeezes out every
- > >microsecond?
-
- So are you saying if people don't have a PowerPC and 24MB of ram, that
- they can't run any software? When a piece of software is a hog, it's
- a hog no matter what hardware you have.
-
- >RN> My horror story is one word long: BASIC.
- >I disagree.
-
- You obviously haven't read other peoples BASIC code. ;-)
- --jc
- --
- Jimen Ching (WH6BRR) jching@aloha.com wh6brr@uhm.ampr.org
-